package com.ruoyi.transaction.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.transaction.domain.Contact;
import com.ruoyi.transaction.mapper.ContactMapper;
import com.ruoyi.transaction.service.IContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContactService implements IContactService {

    @Autowired
    private ContactMapper contactMapper;

    @Override
    public AjaxResult add(Contact contact) {
        int count = contactMapper.insert(contact);
        if(count > 0)
            return  AjaxResult.success("操作成功");
        else
            return AjaxResult.error("操作失败");
    }

    @Override
    public AjaxResult update(Contact contact) {
        int count = contactMapper.updateByPrimaryKeySelective(contact);
        if(count > 0)
            return  AjaxResult.success("操作成功");
        else
            return AjaxResult.error("操作失败");
    }


    @Override
    public AjaxResult deleteByPrimaryKeyAndUserId(Integer contactId, Integer userId) {
        int count = contactMapper.deleteByPrimaryKeyAndUserId(contactId, userId);
        if(count > 0)
            return  AjaxResult.success("操作成功");
        else
            return AjaxResult.error("操作失败");

    }

    @Override
    public List<Contact> listByUserId(Integer userId) {
        return contactMapper.selectByUserId(userId);
    }

    @Override
    public Contact listByContactId(Integer contactId, Integer userId) {
        return contactMapper.selectByPrimaryKeyAndUserId(contactId,userId);

    }

    @Override
    @Transactional
    public AjaxResult changeDefaultContact(Integer contactId, int userId) {

        contactMapper.updateDefaultContactToNormal(userId);
        int count = contactMapper.changeDefaultContact(contactId, userId);
        if(count > 0) {
            return AjaxResult.success("操作成功");
            }else{
            return AjaxResult.error("操作失败");
            }
        }

    @Override
    public Contact selectDefaultContact(int userId) {
       return  contactMapper.selectDefaultContact(userId);

    }
}
